import pygame
from pygame.locals import Rect
from enum import Enum
from random import randint

def getRandomChar() -> str:
    return ['Q', 'W', 'E', 'A', 'S', 'D'][randint(0, 5)]
        

class State(Enum):
    DEFAULT = 0
    ACTIVE = 1
    SUCCESS = 2
    

class KeyObject:
    #static properties
    sysfont = None
    
    def __init__(self, pos=(0,0)):
        if(not KeyObject.sysfont):
            KeyObject.sysfont = pygame.font.SysFont('Sans', 72)
            
        self.key = getRandomChar()
        self.rect = Rect((0, 0, 96, 96))
        self.message = KeyObject.sysfont.render(self.key, True, (255, 255, 255))
        self.messageGray = KeyObject.sysfont.render(self.key, True, (125, 125, 125))
        self.messageRect = self.message.get_rect()
        self.messageRect.center = self.rect.center
        self.rect.move_ip(pos)
        self.messageRect.move_ip(pos)

        self.state = State.DEFAULT.value # 0
        
        
    def reset(self):
        self.state = State.DEFAULT.value
        newChar = getRandomChar()
        self.key = newChar
        
        self.message = KeyObject.sysfont.render(newChar, True, (255, 255, 255))
        self.messageGray = KeyObject.sysfont.render(newChar, True, (125, 125, 125))
        self.messageRect = self.message.get_rect()
        self.messageRect.center = self.rect.center


    def press(self):
        if(self.state == State.ACTIVE.value):
            self.state = State.SUCCESS.value

    def activate(self):
        self.state = State.ACTIVE.value

    def changePos(self, pos):
        self.rect.topleft = pos
        self.messageRect.center = self.rect.center
        
    def render(self):
        from main import SURFACE
        if(self.state == State.DEFAULT.value):
            pygame.draw.rect(SURFACE, (255, 255, 255), self.rect, 3)
            SURFACE.blit(self.message, self.messageRect)
        elif(self.state == State.ACTIVE.value):
            pygame.draw.rect(SURFACE, (255, 255, 0), self.rect, 3)
            SURFACE.blit(self.message, self.messageRect)
        elif(self.state == State.SUCCESS.value):
            pygame.draw.rect(SURFACE, (125, 125, 125), self.rect, 3)
            SURFACE.blit(self.messageGray, self.messageRect)
        
